<?php

/**
 * Class Crontab
 */
class Crontab extends MY_Controller{


    public function up_post(){

    }


    /**
     * 更新用户的发帖子的数量
     * name: up_count
     * @throws Exception
     * @Author:taiyang
     */
    public function up_count(){
        $p=$this->input->get_post('p')?$this->input->get_post('p'):1000;//每次条数
        //查出所有的uid
        $sql="SELECT uid FROM t_post WHERE 1 GROUP BY uid";
        $list=$this->PostModel->query_sql_list($sql);
        $i=0;
        foreach ($list as $k =>$v){
            $i++;
            //统计用户的非删除的帖子
            $sql="SELECT count(*) FROM t_post WHERE uid=".$v['uid'].' and status<3';
            $num=$this->PostModel->query_sql_count($sql);
            $this->UserCount->update_data(array('pub_num'=>$num),array('uid'=>$v['uid']));
            echo $i.'----'.$v['uid']."===>\n";

        }
    }

    /**
     * 打标签
     * name: add_tag
     * @throws Exception
     * @Author:taiyang
     */
    public function add_tag(){
        $p=$this->input->get_post('p')?$this->input->get_post('p'):1000;//每次条数
        //取出所有的话题
        $topic_list=$this->TopicModel->getTopicList();
        //取出所有的标签
        $tag_list=$this->TagModel->getTagList();
        $new_tag=array_column($tag_list,'title','id');
        $new_topic=array_column($topic_list,'title','id');
        $sql="select * from t_post where 1 and (is_audit=0 or is_audit=2) and source='荔枝长音频切割' order by update_time asc limit ".$p;
        $arr=$this->PostModel->query_sql_list($sql);
        foreach ($arr as $k=>$v){
            $this->PostTopicModel->get_tag($v['title'].$v['desc'],$new_topic,$new_tag,$v['id']);
        }
    }


    //查询不合格的图片并标记删除
//UPDATE t_img SET status='-1'
//WHERE 1 and type=1 AND related_id<1 AND (w<720 OR h<720 OR size<20480);



    /**
     * 查询不符合规定的帖子，并修改状态
     * name: update_post
     * @throws Exception
     * @Author:taiyang
     */
    public function update_post(){
        //获取不合规定的帖子
        $sql="select tp.* from t_img ti left join t_post tp on tp.id=ti.related_id where ti.type=1 and ti.related_id>0 and (ti.w<720 or ti.h<720 or ti.size<20480) AND tp.status NOT IN(2,3) limit 50";
        $arr=$this->PostModel->query_sql_list($sql);
        $up_data=array('status'=>2,'is_audit'=>1);
        foreach ($arr as $k=>$v){
            $this->PostModel->update_data($up_data,array('id'=>$v['id']));
            echo $v['id']."===>";
        }
    }

    /**
     * 修改图片库的图片大小和宽高
     * http://console.nuanguang.vip/index.php?c=crontab&m=update_img
     * name: update_img
     * @throws Exception
     * @Author:taiyang
     */
    public function update_img(){
        $type=$this->input->get_post('type')?$this->input->get_post('type'):1;
        $sql="select * from t_img where type =$type and (size IS NULL OR h IS NULL OR w IS NULL OR size =0 OR h =0 OR w =0) order by update_time asc";
        $list=$this->ImgModel->query_sql_list($sql);
        foreach ($list as $k => $v){
            $img_arr=$this->getImageInfo($v['path']);
            $this->ImgModel->update_data($img_arr,array('id'=>$v['id']));
            echo $v['id'].'->'.json_encode($img_arr);
        }
    }

    public function getImageInfo($img) //$img为图像文件绝对路径
    {
        $img_info = getimagesize($img);
        $img_size_arr =get_headers($img,true); //获取文件大小
        $new_img_info = array(
            "w" => $img_info[0],
            "h" => $img_info[1],
            "size" => $img_size_arr['Content-Length']
        );
        return $new_img_info;
    }
}